<?xml version="1.0" encoding="utf-8"?>
<!-- This comment will force IE7 to go into quirks mode. -->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
<link rel="stylesheet" type="text/css" href="../../CSS/Contents.css"></link>
<script type="text/javascript" src="../../JS/Common.js"></script>
<title>Grammar&lt;T&gt; Class</title>
</head>
<body>
<div id="Header">
<div id="ProjectTitle">Documentation Project</div>
<div id="PageTitle">Grammar&lt;T&gt; Class</div>
<div id="HeaderShortcuts">
<a href="#SectionHeader0" onclick="javascript: SetSectionVisibility(0, true); SetExpandCollapseAllToCollapseAll();">Syntax</a>&nbsp;
<a href="#SectionHeader1" onclick="javascript: SetSectionVisibility(1, true); SetExpandCollapseAllToCollapseAll();">Remarks</a>&nbsp;
<a href="#SectionHeader2" onclick="javascript: SetSectionVisibility(2, true); SetExpandCollapseAllToCollapseAll();">Members</a>&nbsp;
<a href="#SectionHeader3" onclick="javascript: SetSectionVisibility(3, true); SetExpandCollapseAllToCollapseAll();">Example</a>&nbsp;
</div>
<div class="DarkLine"></div>
<div class="LightLine"></div>
<div id="HeaderToolbar">
<img id="ExpandCollapseAllImg" src="../../GFX/SmallSquareExpanded.gif" alt="" style="vertical-align: top;" onclick="javascript: ToggleAllSectionsVisibility();" />
<span id="ExpandCollapseAllSpan" onclick="javascript: ToggleAllSectionsVisibility();">Collapse All</span>
</div>
</div>
<div id="Contents">
<a id="ContentsAnchor">&nbsp;</a>
Inherit from this class to implement a typed grammar.
<div id="ItemLocation">
<b>Namespace:</b> <a href="../../Contents/3/249.html">Sasa.Parsing.Pratt</a><br />
<b>Assembly:</b> <a href="../../Contents/1/10.html">Sasa.Parsing</a>
</div>
<div id="SectionHeader0" class="SectionHeader">
<img id="SectionExpanderImg0" src="../../GFX/BigSquareExpanded.gif" alt="Collapse/Expand" onclick="javascript: ToggleSectionVisibility(0);" />
<span class="SectionHeader">
<span class="ArrowCursor" onclick="javascript: ToggleSectionVisibility(0);">
Syntax
</span>
</span>
</div>

<div id="SectionContainerDiv0" class="SectionContainer">
<table class="CodeTable"><col width="100%" /><tr class="CodeTable"><th class="CodeTable">C#</th></tr><tr class="CodeTable"><td class="CodeTable"><table style="width: 100%;" class="InsideCodeBlock">
<col width="0%" />
<col width="100%" />
<tr>
<td class="NoWrapTop">public abstract class Grammar&lt;T&gt; </td>
<td>&nbsp;</td>
</tr>
</table>
</td></tr></table>
<div class="CommentHeader">Type Parameters</div>
<div class="CommentParameterName">T</div>
<div class="ParameterCommentContainer">
The type of values parsed from the input.
</div>
<div class="TopLink"><a href="#ContentsAnchor">Top</a></div></div>
<div id="SectionHeader1" class="SectionHeader">
<img id="SectionExpanderImg1" src="../../GFX/BigSquareExpanded.gif" alt="Collapse/Expand" onclick="javascript: ToggleSectionVisibility(1);" />
<span class="SectionHeader">
<span class="ArrowCursor" onclick="javascript: ToggleSectionVisibility(1);">
Remarks
</span>
</span>
</div>

<div id="SectionContainerDiv1" class="SectionContainer">
<div class="RemarksContainer">
Implements a simple single-state Pratt-style parser with a longest
            match precedence-based lexer. Clients need only inherit from this class,
            specify the type of elements being parsed, and in the constructor function
            specifying the set of parsable operators with the associated semantic action.
            
            Pratt-parsers are effectively Turing complete, so they can parse any grammar imaginable,
            although the predefined combinators encourage context-free grammars.
            
            References:
            <ul><li><a href="http://effbot.org/zone/simple-top-down-parsing.htm">http://effbot.org/zone/simple-top-down-parsing.htm</a></li><li><a href="http://javascript.crockford.com/tdop/tdop.html">http://javascript.crockford.com/tdop/tdop.html</a></li></ul>
</div>
<div class="TopLink"><a href="#ContentsAnchor">Top</a></div></div>
<div id="SectionHeader2" class="SectionHeader">
<img id="SectionExpanderImg2" src="../../GFX/BigSquareExpanded.gif" alt="Collapse/Expand" onclick="javascript: ToggleSectionVisibility(2);" />
<span class="SectionHeader">
<span class="ArrowCursor" onclick="javascript: ToggleSectionVisibility(2);">
Members
</span>
</span>
</div>

<div id="SectionContainerDiv2" class="SectionContainer">
<p>Click <a href="../../Contents/3/264.html">here</a> to see the list of members.</p>
<div class="TopLink"><a href="#ContentsAnchor">Top</a></div></div>
<div id="SectionHeader3" class="SectionHeader">
<img id="SectionExpanderImg3" src="../../GFX/BigSquareExpanded.gif" alt="Collapse/Expand" onclick="javascript: ToggleSectionVisibility(3);" />
<span class="SectionHeader">
<span class="ArrowCursor" onclick="javascript: ToggleSectionVisibility(3);">
Example
</span>
</span>
</div>

<div id="SectionContainerDiv3" class="SectionContainer">
Here is a simple calculator as an example:
            <table class="ExampleCodeTable"><col width="100%" /><tr class="CodeTable"><th class="CodeTable">C#</th></tr><tr class="CodeTable"><td class="CodeTable"><pre>class Calculator : Grammar&lt;int&gt;
{
    public Calculator()
    {
        Infix("+", 10, Add);   Infix("-", 10, Sub);
        Infix("*", 20, Mul);   Infix("/", 20, Div);
        InfixR("^", 30, Pow);  Postfix("!", 30, Fact);
        Prefix("-", 100, Neg); Prefix("+", 100, Pos);
        Group("(", ")", int.MaxValue);
        Match("(digit)", char.IsDigit, 1, Int);
        SkipWhile(char.IsWhiteSpace);
    }

    int Int(string lit) { return int.Parse(lit); }
    int Add(int lhs, int rhs) { return lhs + rhs; }
    int Sub(int lhs, int rhs) { return lhs - rhs; }
    int Mul(int lhs, int rhs) { return lhs * rhs; }
    int Div(int lhs, int rhs) { return lhs / rhs; }
    int Pow(int lhs, int rhs) { return (int)Math.Pow(lhs, rhs); }
    int Neg(int arg) { return -arg; }
    int Pos(int arg) { return arg; }
    int Fact(int arg)
    {
        return arg == 0 || arg == 1 ? 1 : arg * Fact(arg - 1);
    }
}
</pre></td></tr></table>
<div class="TopLink"><a href="#ContentsAnchor">Top</a></div></div>
</div>
<div id="Footer">
<span class="Footer">Generated by <a href="http://immdocnet.codeplex.com/" target="_blank">ImmDoc .NET</a></span>.
</div>

</body>

</html>
